//
//  NextParamsPageVC.swift
//  SwfitDemo
//
//  Created by menchao on 2018/9/27.
//  Copyright © 2018年 Test. All rights reserved.
//

import UIKit


typealias PassValueBlock = (Int,Int) ->Int
typealias PassStringValueBlock = (String) ->Void




class NextParamsPageVC: UIViewController {

    var parmas:String?
    
    var btnClickBlock:(()->())?

    var passValueBlock:PassStringValueBlock?
    
    var passValue:PassStringValueBlock?
    
 
   
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = self.parmas
        self.view.addSubview(self.clickButton)

        //正常闭包写法
        self.postData2(url: "http://www.baidu.com", sucess: {
            (string) in
              print("postData2 result: \(string)")
        })
        
        
        //简化写法
        self.postData(url: "http://www.baidu.com") { (result) in
            print("postData result: \(result)")
        }
        
        self.postData3(url: "http://www.sohu.com", sucess: nil)
        
        
        self.escapingFunc { (str) in
            print("逃逸闭包获取到了值: \(str)")
        }
        
        self.postData3(url: "http://www.sohu.com", sucess: { (sucess) in
             print("sucess: \(sucess)")
            
        }) { (faile) in
             print("faile: \(faile)")
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    deinit {
        print("NextParamsPageVC is deinit")
    }
    
    // MARK: - 1.闭包尾随写法，作为最后一个参数时，可以省略前面的括号
    func postData(url: String,sucess:(String)->Void) {
        print("发送请求");
        sucess("请求完成")
    }
    
    func postData2(url: String,sucess:PassStringValueBlock) {
        print("发送请求");
        sucess("请求完成")
    }
    
    func postData3(url: String,sucess:PassStringValueBlock?) {
        print("发送请求");
        if sucess != nil{
            sucess!("请求完成")
        }
    }
    
    func postData3(url: String,sucess:PassStringValueBlock?,faile:PassStringValueBlock?) {
        print("发送请求");
        if sucess != nil{
            sucess!("请求完成")
        }
        if faile != nil{
            faile!("请求失败")
        }
    }
    
    
    // MARK: - 2.逃逸闭包
    func escapingFunc(sucess :@escaping PassStringValueBlock){
        self.passValueBlock = sucess
    }
    
    
    @objc func clickMe(){
        if self.passValue != nil{
            self.passValue!("我传了参数呵呵")
        }
        
        if  self.passValueBlock  != nil{
            self.passValueBlock!("逃逸闭包成功")
        }
    }
    
    
    lazy var clickButton: UIButton = {
        let clickButton = UIButton(frame: CGRect.init(x: 0, y: 100, width: 300, height: 40))
        clickButton.setTitle("我要回传参数到上一个页面", for: UIControlState.normal)
        clickButton.backgroundColor = UIColor.blue
        clickButton.addTarget(self, action: #selector(clickMe), for: UIControlEvents.touchUpInside)
        return clickButton
    }()

}
